#include <bits/stdc++.h>

class Solution {

    int dx[4] = { -1, 0, 1, 0 };
    int dy[4] = { 0, 1, 0, -1 };

    int n, m;
    int count;
    int vis[210][210];


public:

    int solve(vector<vector<char> >& grid) {

        n = grid.size(), m = grid[0].size();

        queue<pair<int, int>> que;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == '1' && vis[i][j] == 0)
                {
                    count++;

                    que.push({ i, j });

                    while (!que.empty())
                    {
                        int sz = que.size();

                        while (sz--)
                        {
                            auto [x, y] = que.front();
                            que.pop();

                            for (int i = 0; i < 4; i++)
                            {
                                int x1 = x + dx[i];
                                int y1 = y + dy[i];

                                if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && grid[x1][y1] == '1' && vis[x1][y1] == 0)
                                {
                                    que.push({ x1, y1 });
                                    vis[x1][y1] = 1;
                                }
                            }
                        }
                    }
                }
            }
        }
        return count;
    }
};